﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShieldNet.DB;
using ShieldNet.Models;
using System.Data;

namespace ShieldNet.Data
{
    public interface IDataSpam 
    {
        SpamsInDayObject Select_SpamsInfo_InDay(int top, int id);

        List<SpamsObject> Select_Spams_Paging(int OffsetRows, int FetchRows);

        TableSpams Get_TableSpams(int offset, int limit);
    }

    public class DataSpam : IDataSpam
    {
        //private readonly MainDB db = new MainDB();
        public SpamsInDayObject Select_SpamsInfo_InDay(int top, int id)
        {
            MainDB db = new MainDB();
            var obj = new SpamsInDayObject();
            try
            {
                DataSet ds = new DataSet();
                ds = db.StoredProcedures.Select_SpamsInfo_InDay(id, top);
                if (ds.Tables != null)
                {
                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        var spam = new SpamsObject();
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            spam = new SpamsObject
                            {
                                id = Convert.ToInt32(dr["id"]),
                                msisdn = dr["msisdn"].ToString(),
                                os = dr["os"].ToString(),
                                report_dev_id = dr["report_dev_id"].ToString(),
                                spam_datetime = Convert.ToDateTime(dr["spam_datetime"]),
                                spam_group = Convert.ToInt32(dr["spam_group"]),
                                spam_purpose = dr["spam_purpose"].ToString(),
                                spam_text = dr["spam_text"].ToString()
                            };
                            obj.SpamsObject.Add(spam);
                        }
                    }
                    //else
                    //{
                    //    obj.SpamsObject = Select_SpamsInfo_PreDay(top);
                    //}

                    obj.TotalDay = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                    obj.Total = Convert.ToInt32(ds.Tables[2].Rows[0][0]);
                }
            }
            catch (Exception ex)
            {
                obj.Message = ex.Message;
            }
            db.Dispose();
            db.Close();
            return obj;
        }

        public List<SpamsObject> Select_SpamsInfo_PreDay(int top)
        {
            MainDB db = new MainDB();
            var obj = new List<SpamsObject>();
            DataTable dt = db.StoredProcedures.Select_SpamsInfo_PreDay(top);
            if (dt != null && dt.Rows.Count >0)
            {
                var spam = new SpamsObject();
                foreach (DataRow dr in dt.Rows)
                {
                    spam = new SpamsObject
                    {
                        id = Convert.ToInt32(dr["id"]),
                        msisdn = dr["msisdn"].ToString(),
                        os = dr["msisdn"].ToString(),
                        report_dev_id = dr["report_dev_id"].ToString(),
                        spam_datetime = Convert.ToDateTime(dr["spam_datetime"]),
                        spam_group = Convert.ToInt32(dr["spam_group"]),
                        spam_purpose = dr["spam_purpose"].ToString(),
                        spam_text = dr["spam_text"].ToString()
                    };
                    obj.Add(spam);
                }
            }
            db.Dispose();
            db.Close();
            return obj;
        }

        public List<SpamsObject> Select_Spams_Paging(int OffsetRows, int FetchRows)
        {
            MainDB db = new MainDB();
            var obj = new List<SpamsObject>();
            try
            {
                DataTable dt = new DataTable();
                dt = db.StoredProcedures.Select_Spams_Paging(OffsetRows, FetchRows);
                if (dt != null && dt.Rows.Count > 0)
                {
                    var spam = new SpamsObject();
                    foreach (DataRow dr in dt.Rows)
                    {
                        spam = new SpamsObject
                        {
                            id = Convert.ToInt32(dr["id"]),
                            msisdn = dr["msisdn"].ToString(),
                            os = dr["os"].ToString(),
                            report_dev_id = dr["report_dev_id"].ToString(),
                            spam_datetime = Convert.ToDateTime(dr["spam_datetime"]),
                            spam_group = Convert.ToInt32(dr["spam_group"]),
                            spam_purpose = dr["spam_purpose"].ToString(),
                            spam_text = dr["spam_text"].ToString()
                        };
                        obj.Add(spam);
                    }
                }               
            }
            catch (Exception ex)
            {
                
            }
            db.Dispose();
            db.Close();
            return obj;
        }

        public TableSpams Get_TableSpams(int offset, int limit)
        {
            MainDB db = new MainDB();
            var obj = new TableSpams();

            try
            {                
                DataTable dt = new DataTable();
                dt = db.StoredProcedures.Select_Spams_Paging(offset, limit);
                if (dt != null && dt.Rows.Count > 0)
                {
                    var spam = new SpamsObject();
                    foreach (DataRow dr in dt.Rows)
                    {
                        spam = new SpamsObject
                        {
                            id = Convert.ToInt32(dr["id"]),
                            msisdn = dr["msisdn"].ToString(),
                            os = dr["os"].ToString(),
                            report_dev_id = dr["report_dev_id"].ToString(),
                            spam_datetime = Convert.ToDateTime(dr["spam_datetime"]),
                            spam_group = Convert.ToInt32(dr["spam_group"]),
                            spam_purpose = dr["spam_purpose"].ToString(),
                            spam_text = dr["spam_text"].ToString()
                        };
                        obj.rows.Add(spam);
                    }
                }               
            }
            catch (Exception ex)
            {

            }
            db.Dispose();
            db.Close();

            return obj;
        }
    }
}